/*
 * Copyright 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.activemanager.dashboard;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.activemanager.digitalTrain.WelcomeCalibrating_DT0;
import com.activemanager.gearCenter.GearCenter;
import com.activemanager.healthGadget.HealthGadget;
import com.activemanager.myHealthProfile.Q1MainActivity;
import com.activemanager.workoutAssistant.WorkoutAssistant_W1;
import com.activemanager.workoutAssistant.WorkoutAssistant_W2;
import com.activemanager.workoutAssistant.WorkoutAssistant_W3;
import com.activemanager.workoutAssistant.WorkoutHistory_C1;
import com.example.android.navigationdrawerexample.R;
import com.example.android.navigationdrawerexample.R.array;
import com.example.android.navigationdrawerexample.R.drawable;
import com.example.android.navigationdrawerexample.R.id;
import com.example.android.navigationdrawerexample.R.layout;

import android.app.Activity;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class H0Activity extends FragmentActivity {
	private DrawerLayout mDrawerLayout;
	private LinearLayout mLinearLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] mPlanetTitles;
	private Button mButton;
	private Button btnProfile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		setContentView(R.layout.activity_main);
		
		
//		DisplayMetrics metric = new DisplayMetrics();  
//        getWindowManager().getDefaultDisplay().getMetrics(metric);  
//        int width = metric.widthPixels;     // ��Ļ��ȣ����أ�  
//        int height = metric.heightPixels;   // ��Ļ�߶ȣ����أ�  
//        float density = metric.density;      // ��Ļ�ܶȣ�0.75 / 1.0 / 1.5��  
//        int densityDpi = metric.densityDpi;  // ��Ļ�ܶ�DPI��120 / 160 / 240��  
//
//        Log.d("tiandandan", "width height density densityDpi" + width + " || " + height + " || " + density + " || " + densityDpi + " || ");
//        System.out.println("width height density densityDpi" + width + " || " + height + " || " + density + " || " + densityDpi + " || ");
//        
//        

		mTitle = mDrawerTitle = getTitle();
		mPlanetTitles = getResources().getStringArray(R.array.planets_array);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
//		InputStream is = getResources().openRawResource(R.drawable.u0_normal);
//		Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//		mDrawerLayout.setBackgroundDrawable(new BitmapDrawable(bit));
		mDrawerLayout.setBackgroundResource(R.drawable.bg_540);
		
		mLinearLayout = (LinearLayout) findViewById(R.id.left_drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);

		mButton = (Button) findViewById(R.id.button1);

		mButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mDrawerLayout.openDrawer(Gravity.LEFT);
			}

		});
		
		Button btnProfile_cancel = (Button) findViewById(R.id.imageView2);
		btnProfile_cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mDrawerLayout.closeDrawer(mLinearLayout);
			}
		});

		btnProfile = (Button) findViewById(R.id.imageView1);
		btnProfile.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mDrawerLayout.closeDrawer(mLinearLayout);
				new Handler().postDelayed(new Runnable(){
					
					@Override
					public void run() {
						Intent intent = new Intent();
						intent.setClass(H0Activity.this, H2Activity.class);
						startActivity(intent);
						}
					},300);
			}

		});

		// set a custom shadow that overlays the main content when the drawer
		// opens
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		// set up the drawer's list view with items and click listener
		String[] mFrom = new String[] { "profile_img", "profile_item" };
		String[] profileTitle = { "Workout Assistant","Digital Trainging",
				"Health Profile", "Gear center",
				"Workout Calendar", "Health Gadget", "About" };
		int[] mTo = new int[] { R.id.profile_img, R.id.profile_item };
		List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
		Map<String, Object> mMap = null;
		for (int i = 0; i < profileTitle.length; i++) {
			mMap = new HashMap<String, Object>();
			int drawable = getResources().getIdentifier(
					"uu" + (68 + i) + "_normal", "drawable",
					this.getPackageName());
			mMap.put("profile_img", drawable);
			mMap.put("profile_item", profileTitle[i]);
			mList.add(mMap);
		}
		SimpleAdapter mAdapter = new SimpleAdapter(this, mList,
				R.layout.drawer_list_item, mFrom, mTo);
		mDrawerList.setAdapter(mAdapter);
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

		if (savedInstanceState == null) {
			selectItem(8);
		}
	}

	/* The click listner for ListView in the navigation drawer */
	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			System.out.println(position);
			selectItem(position);
		}
	}

	private void selectItem(final int position) {
		// update the main content by replacing fragments
		// Fragment fragment = new PlanetFragment();
		//
		// Bundle args = new Bundle();
		//
		// args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
		// System.out.println(args.toString());
		// fragment.setArguments(args);
		//
		// FragmentManager fragmentManager = getFragmentManager();
		// fragmentManager.beginTransaction().replace(R.id.content_frame,
		// fragment).commit();
		//
		// // update selected item and title, then close the drawer
		// mDrawerList.setItemChecked(position, true);
		// setTitle(mPlanetTitles[position]);
		// mDrawerLayout.closeDrawer(mLinearLayout);
		// my health profile, workout assistant, digital training, gear center,
		// health gadget,
//		String[] dashBoardFragment = {
//				"com.activemanager.dashboard.H0Fragment", 
//				"com.activemanager.myHealthProfile.Q1MainFragment",
//				"com.activemanager.workoutAssistant.WorkoutAssistant_W1",
//				"com.activemanager.digitalTrain.WelcomeCalibrating_DT0",
//				"com.activemanager.healthGadget.HealthGadget",
//				"com.activemanager.healthGadget.HealthGadget",
//				"com.activemanager.healthGadget.HealthGadget",
//				"com.activemanager.dashboard.AboutFragment" };
//		mDrawerList.setItemChecked(position, true);
		mDrawerLayout.closeDrawer(mLinearLayout);
		final FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
		final Intent intent = new Intent();
		new Handler().postDelayed(new Runnable(){
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (position == 0) {
					
					final Dialog dialog = new Dialog(H0Activity.this,
							R.style.MyDialog);
					final BitmapFactory.Options opt = new BitmapFactory.Options();
					opt.inPreferredConfig = Bitmap.Config.RGB_565;
					opt.inPurgeable = true;
					opt.inInputShareable = true;

					dialog.setContentView(R.layout.p30);
					final LinearLayout img = (LinearLayout) dialog
							.findViewById(R.id.linearLayout);
//					InputStream is = getResources().openRawResource(R.drawable.u165_normal);
//					Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//					img.setBackgroundDrawable(new BitmapDrawable(bit));
					img.setBackgroundResource(R.drawable.bg_for_popup_54);

					dialog.show();
					Window dialogWindow = dialog.getWindow();
					WindowManager m = getWindowManager();
					Display d = m.getDefaultDisplay();
					WindowManager.LayoutParams p = dialogWindow.getAttributes();
					p.height = (int) (d.getHeight() * 0.765);
					p.width = (int) (d.getWidth() * 0.95);
					dialogWindow.setAttributes(p);

					String[] mFrom = new String[] { "profile_img", "profile_item" };
					String[] profileTitle = { "Jogging", "Badminton", "Badminton PRO",
							"Cycling", "Walking", "Dance" };
					int[] mTo = new int[] { R.id.profile_img, R.id.profile_item };
					List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
					Map<String, Object> mMap = null;
					for (int i = 0; i < profileTitle.length; i++) {
						mMap = new HashMap<String, Object>();
						int drawable = getResources().getIdentifier(
								"choose_u" + (155 + i) + "_normal", "drawable",
								H0Activity.this.getPackageName());
						mMap.put("profile_img", drawable);
						mMap.put("profile_item", profileTitle[i]);
						mList.add(mMap);
					}
					final ListView mListView = (ListView) dialog
							.findViewById(R.id.list_view);
					SimpleAdapter mAdapter = new SimpleAdapter(H0Activity.this,
							mList, R.layout.drawer_list_item_for_sports, mFrom, mTo);
					mListView.setAdapter(mAdapter);
					mListView.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
								long arg3) {
							// TODO Auto-generated method stub
							Intent intent = new Intent();
							if (pos == 0) {
								intent.setClass(H0Activity.this,
										WorkoutAssistant_W1.class);
								startActivity(intent);
								dialog.dismiss();
							} else if (pos == 1) {
								intent.setClass(H0Activity.this,
										WorkoutAssistant_W2.class);
								startActivity(intent);
								dialog.dismiss();
							}else if (pos == 2) {
								intent.setClass(H0Activity.this,
										WorkoutAssistant_W3.class);
								startActivity(intent);
								dialog.dismiss();
							}
						}
					});
					
					Button btn_cancel = (Button) dialog
							.findViewById(R.id.button1);
					btn_cancel.setOnClickListener(new OnClickListener(){

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							dialog.dismiss();
						}
					});
					
				
				}
				else if(position ==1){
					intent.setClass(H0Activity.this, WelcomeCalibrating_DT0.class);
					startActivity(intent);
				}
				else if(position ==2){
					intent.setClass(H0Activity.this, Q1MainActivity.class);
					startActivity(intent);
				}
				else if(position ==3){
					intent.setClass(H0Activity.this, GearCenter.class);
					startActivity(intent);
				}
				else if(position ==4){
					intent.setClass(H0Activity.this, WorkoutHistory_C1.class);
					startActivity(intent);
				}
				else if(position ==5){
					intent.setClass(H0Activity.this, HealthGadget.class);
					startActivity(intent);
				}
				else if(position ==6){
					intent.setClass(H0Activity.this, AboutActivity.class);
					startActivity(intent);
				}
				else{
					Fragment tmp = Fragment.instantiate(H0Activity.this, "com.activemanager.dashboard.H0Fragment");
			        tx.replace(R.id.content_frame,tmp);
			        tx.commit();
				}
			}	
		}, 350);
		
		
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		System.out.println(mTitle);
		getActionBar().setTitle(mTitle);
	}

	/**
	 * Fragment that appears in the "content_frame", shows a planet
	 */
	// public static class PlanetFragment extends Fragment {
	// public static final String ARG_PLANET_NUMBER = "planet_number";
	//
	// public PlanetFragment() {
	// // Empty constructor required for fragment subclasses
	// }
	//
	// @Override
	// public View onCreateView(LayoutInflater inflater, ViewGroup container,
	// Bundle savedInstanceState) {
	// View rootView = inflater.inflate(R.layout.fragment_planet, container,
	// false);
	// int i = getArguments().getInt(ARG_PLANET_NUMBER);
	// System.out.println(i);
	// String planet = getResources().getStringArray(R.array.planets_array)[i];
	//
	// int imageId =
	// getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
	// "drawable", getActivity().getPackageName());
	// // ((ImageView)
	// rootView.findViewById(R.id.image)).setImageResource(imageId);
	// System.out.println(planet);
	// getActivity().setTitle(planet);
	// return rootView;
	// }
	// }
	
	
}